import pandas as pd
import numpy as np
import json
import os

def generate_dynamic(db, ob_win, features):
    #choose the features
    dynamic_data = db[features].values
    #get the length of row
    data_length = dynamic_data.shape[0]
    #return the data
    for start,stop in zip(range(0, data_length-ob_win), range(ob_win, data_length)):
        yield dynamic_data[start:stop, :]


def generate_static(db, ob_win, features):
    #choose the features
    static_data = db[features].values
    #get the length of row
    data_length = static_data.shape[0]
    #return the data after ob_win time
    return static_data[ob_win-1 : data_length-1, :]

def generate_labels(db, ob_win, features):
    #choose the label feature
    label_data = db[features].values
    #get the length of row
    data_length = label_data.shape[0]
    #return the data after ob_win time
    return label_data[ob_win-1 : data_length-1, :]

#本函数返回静态数据static_data，动态数据 dynamic_datal，标签数据abel_data
def generate_data(db, ob_win, static_features, dynamic_features, label_features):
    #static,dynamic,label = generate_data(db数据集, length=15,static_features=config["static"],
    #                                   dynamic_features=config["dynamic"], label_features=config["label"])
    grouped = db.groupby("caseid")
    #static data静态数据
    print("static data...")
    #type : list
    #static_data = [list(generate_static(group ob_win=15, static_features=config["static"])) for id in db["caseid"].unique()]
    static_data = [list(generate_static(group, ob_win, static_features)) for value, group in grouped]
    #drop []
    static_data = [sd for sd in static_data if sd != []]#不含空值的列表
    #type list to type array
    static_data = np.concatenate(static_data).astype(np.float32)#转换数据格式
    #static_data = np.concatenate(static_data)

    #label_data 标签数据
    print("label data...")
    #label_data = [list(generate_labels(group, ob_win=15, label_features=config["label"])) for id in db["caseid"].unique()]
    label_data = [list(generate_labels(group, ob_win, label_features)) for value, group in grouped]
    #drop []
    label_data = [ld for ld in label_data if ld != []]#不含空值的列表
    #type list to type array
    label_data = np.concatenate(label_data).astype(np.float32)#转换数据格式

    #dynamic data 动态数据
    print("dynamic data...")
    #type : list
    #dynamic_data = [list(generate_dynamic(db[db["caseid"]==id], ob_win, dynamic_features)) for id in db["caseid"].unique()]
    dynamic_data = [list(generate_dynamic(group, ob_win, dynamic_features)) for value, group in grouped]
    #drop []
    dynamic_data = [dd for dd in dynamic_data if dd != []]#不含空值的列表
    #type list to type array
    dynamic_data = np.concatenate(dynamic_data).astype(np.float32)#转换数据格式

    return static_data, dynamic_data, label_data

def process(static, dynamic, label):
    dynamic = np.delete(dynamic, [i for i,x in enumerate(label) if x==[2]], 0)
    static = np.delete(static, [i for i,x in enumerate(label) if x==[2]], 0)
    label = np.delete(label, [i for i,x in enumerate(label) if x==[2]], 0)
    #按行删除x==[2]位置的数据
    return static, dynamic, label

#返回处理之后的数据（加载，删除选中）
def gen_data(s_path, d_path, length=15):
    '''
        {
            "train_path": "static/train_case.csv",
            "static_features": null,
            "dynamic_features": ["MAP", "SDP", "HR"],
            "label_features": ["label"]
        }
    '''
    with open("config.json", encoding="utf-8") as config_file:
        config = json.load(config_file)
        static_features = config["static"]
        dynamic_features = config["dynamic"]
        label_features = config["label"]
    print("reading data...")
    static_df = pd.read_csv(s_path)#读取文件
    dynamic_df = pd.read_csv(d_path)#读取文件
    print("merging data...")#pandas合并数据集
    df = pd.merge(static_df, dynamic_df, how="inner", on=["caseid"])
    print("generating data...")
    #得到静态数据static_data，动态数据 dynamic_datal，标签数据abel_data
    static, dynamic, label = generate_data(df, length, static_features, dynamic_features, label_features)
    print("processing data...")
    static, dynamic, label = process(static, dynamic, label)#执行选中删除操作
    print(label.sum(), label.shape[0])
    return static, dynamic, label

def read_data(name="tongji", config="30-5-20", length=15, mode="train"):

    #d_path = "data/tongji/30-5-20.csv"
    d_path = "data/" + name + "/" + config + ".csv"
    #s_path = "data/tongji/static_train.csv"
    s_path = "data/" + name + "/static" + "_" + mode +".csv"
    return gen_data(s_path, d_path, length)
    



